[Coding042] LeetCode 71

Simplify Path

Cook 2024.04.10

More coding records

Get the knowledge flowing and circulating! :)

目录

本题收获

小知识收获:栈的for循环遍历 | StringBuffer | String字符串的判等

思路收获:一开始写这道题的时候,就按照常规思路来思考,/的处理给我带了很大的困扰,然后按照测试样例逐步完善了我的思路。最后还是没能写出代码。看了以前的程序发现处理/的思维是:既然每层之间都要有/,不防把文件名取出,特判处理., ..的情况,最后在输出答案的时候人为地在目录之间加上/即可

大大简化了问题,最后得到了答案。很棒!

题目:71. Simplify Path

Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.

The canonical path should have the following format:

Return the simplified canonical path.

Example 1:

Example 2:

Example 3:

Constraints:


代码1(配 · 手绘过程图)

image-20240411160722967

代码解读 | 评价

这个代码写起来还是比较难的,把问题复杂化了。

但是一点点的测试样例让自己的思维越来越丰富,可以明显的发现自己的逻辑思考能力有了些提升。

复杂度分析

image-20240411155757226


代码2(配 · 手绘过程图)

代码解读 | 评价

  • 代码2比代码1更简洁;

  • 代码2更能磨练一个人的思维,更清晰;

复杂度分析

image-20240411155633196


 

测试样例

知识点积累 (Java)

  1. 字符串分割

  2. 判断字符串相等

  3. StringBuffer